<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/25
 * Time: 下午2:13
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class PosterController extends AdminBaseController{
    /**
     * 广告版位列表
     */
    public function index(){
        $spaceid = I('get.spaceid/d',0);
//        echo $spaceid;
        $list = M('poster')->alias('a')->field('a.*,s.name')->join('left join dx_posterspace s on a.spaceid=s.id') -> select();
        $this -> assign('list',$list);
        $this -> display();
    }
    public function pageList(){
        $page = I('get.page/d',1);

        $rule = M('rule');
        //  总记录数
        $count = $rule -> count();
        $limit = 20;
        //  总页数
        $pages = ceil($count/$limit);
        $start = empty($page - 1) ? 0 : (($page - 1) * $limit);

        $list = $rule -> order('id') -> limit($start . ',' . $limit) -> select();

        $list = get_tree($list, 0, 0, '----&nbsp;');
        $content = '';
//        foreach($list as $value){
//            $content .= '<tr><td>' . $value['id'] . '</td><td>' . $value['html'] . $value['title'] . '</td><td>' . $value['name'] . '</td><td>';
//            if($value['status']){
//                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-primary" id="status' . $value['id'] . '">正常</a>';
//            }else{
//                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-danger" id="status' . $value['id'] . '">禁用</a>';
//            }
//            $content .= '</td><td><div class="col-md-12"><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Rule/add/pid/' .$value['id']) . '"><i class="fa fa-edit"></i> 添加子权限</a><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Rule/edit/id/' .$value['id']) . '"><i class="fa fa-edit"></i> 修改</a><a class="btn btn-sm btn-outline btn-danger" href="javascript:del(' . $value['id'] . ')"><i class="fa fa-trash-o"></i> 删除</a></div></td></tr>';
//        }
        $data = [
            'pages' => $pages,
            'content' => $content,
        ];
        echo json_encode($data);
    }

    /**
     * 广告添加表单(与修改共用了同一个模版文件)
     */
    public function add(){
        $spaceid = I('get.spaceid/d',0);
        $spaceInfo = M('posterspace') -> field('id,type,name') -> where(['id' => $spaceid]) -> find();
        $this->assign('spaceInfo',$spaceInfo);
        $this->display();
    }
    /**
     * 广告修改表单(与添加共用了同一个模版文件)
     */
    public function edit(){
        $id = I('get.id/d',0);
        $poster = M('poster')->where(['id' => $id])->find();
        $spaceInfo = M('posterspace') -> field('id,type,name') -> where(['id' => $poster['spaceid']]) -> find();
//        dump($spaceInfo);dump($poster);exit;
        $this->assign('spaceInfo',$spaceInfo);
        $this->assign('data',$poster);
        $this->display('poster_add');
    }

    /**
     * 修改及保存
     */
    public function Save(){
        if(IS_AJAX && IS_POST){
//            dump($_POST);exit;
//            dump($_FILES);
            /**
             * 过滤信息
             */
            $id = I('post.id/d',0);
            $spaceid = I('post.spaceid/d',0);
            $status = I('post.status/d',0);
            $type = I('post.type1/d',0);
            $title = I("post.title/s",'');
            if(!$title){
                $this->ajaxReturn(['status' => 1, 'msg' => '广告标题不能为空！']);
            }
            $sort = I('post.sort/d',0);
            if(!is_int($sort)){
                $this->ajaxReturn(['status' => 1, 'msg' => '排序不符合要求']);
            }
            $start = I('post.start/s','');
            if(!$start){
                $this->ajaxReturn(['status' => 1, 'msg' => '上线时间不能为空']);
            }
            $end = I('post.end/s','');
            if(!$end){
                $this->ajaxReturn(['status' => 1, 'msg' => '上线时间不能为空']);
            }


            $poster = M('poster');
            $setting = $poster -> field('setting') -> where(['id' => $id]) -> find();
            $setting = $setting['setting'];

            if(0 != $_FILES['setting']['size']){
                $setting = uploadFile($_FILES['setting'],'Upload/poster/');
            }

            if(is_array($setting)){
                $this->ajaxReturn($setting);
            }


            $data = $poster -> where(['title' => $title]) -> find();
//            echo $data . '<br>' . $id;exit;
            if(!empty($data)){
                if($data['id'] != $id){
                    $this->ajaxReturn(['status' => 1, 'msg' => '广告名称已经存在']);
                }
            }
            $data = [
                'id' => $id,
                'sort' => $sort,
                'spaceid' => $spaceid,
                'title' => $title,
                'setting' => $setting,
                'startdate' => $start,
                'enddate' => $end,
                'status' => $status,
                'type' => $type,
            ];
//            dump($data);exit;
            $isSQl = $poster -> add($data,[],true);
            if(!$isSQl){
                $this->ajaxReturn(['status' => 1, 'msg' => '失败！']);
            }
                $this->ajaxReturn(['status' => 0, 'msg' => '成功！']);
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }

    /**
     * 更改权限状态
     */
    public function statusChange(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
            $rule = M('poster');
            $r = $rule ->field('status') -> where(['id' => $id]) -> find();
            if(1 == $r['status']){
                $rule -> where(['id' => $id]) -> setField('status', 0);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已禁用', 'data' => 0]);
            }else{
                $rule -> where(['id' => $id]) -> setField('status', 1);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已开启', 'data' => 1]);
            }
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }

    /**
     * 删除权限
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
            }
            $isDel = M('poster')->where(['id' => $id])->delete();
            if(!$isDel){
                $this->ajaxReturn(['status' => 1 , 'msg' => '删除失败！']);
            }
            $this->ajaxReturn(['status' => 0 , 'msg' => '删除成功！']);
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
        }
    }
}